(function () {

  const refreshbtn = document.getElementById("refresh") || {};
  const updatebtn = document.getElementById("update") || {};

  const id = document.getElementById('pet-select');

  function msSleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
  };


  document.getElementById('pet-select').onchange = (e) => {
     console.log("now to select ")
      getdetail()
  };


  var timeBox;

  function getdetail(){

    checkbox = document.getElementById('subscribeNews');
    if (checkbox.checked ) {
    }else{
      clearTimeout(timeBox);
    }

    var d = new Date();
    var dStr = d.toLocaleString()

    let id = document.getElementById('pet-select');
    console.log("now query id  :", id.value )
    $.ajax({
      type : "POST",
      url : 'getdetail', 
      data : JSON.stringify({id:id.value}),
      contentType : "application/json;charset=utf-8",
      //dataType : "json",
      success : function(data){
              console.log("response",data);
              if(data['code'] == "success"){

                    notesHtml = "<tr><td>标题</td><td>发布人</td><td>评论数</td> <td>解析完成</td></tr>"
                    commentsHtml = "<tr><td>序号</td><td>评论内容</td><td>评论时间</td><td>昵称</td><td>发布人二维码</td><td>帖子链接</td></tr>"

                    for(var tmpN in data['notes']){

                      note = data['notes'][tmpN]
                      var tmp = "<tr><td>" + note['titile']+"</td><td>" + note['nickName'] +"</td><td>"+ note['likeCount'] +"</td><td>"+ note['finish'] +"</td></tr>"
                      notesHtml = notesHtml + tmp
                    }
                    
                    for(var c in data['comments']){
                      
                      comment = data['comments'][c][1]
                      console.log("the comments : ",comment)
                      tmp = "<tr><td>"+ comment['no']+ "</td><td>" + comment['content'] + "</td><td>" + comment['local_time'] + "</td><td>" + comment['nickname']+ "</td><td> <img src='../"+ comment['img'] + "' width='50' height='50'/></td><td><a href='" + comment['note'] +  "' target='_blank' >查看原帖</a></td></tr>"
                      commentsHtml = commentsHtml + tmp
                    }

                  document.getElementById("notes").innerHTML=notesHtml
                  document.getElementById("comments").innerHTML=commentsHtml
                  document.getElementById("time").value = dStr

                  document.getElementById("keyword").value = data['config']['keywords']
                  document.getElementById("commenttime").value = data['config']['validTime']
                  document.getElementById("limit_count").value = data['config']['limit_count']
                  document.getElementById("cookie").value = data['config']['cookie']
                  document.getElementById("msg").value = data['config']['msg']
              }else{
                alert(data)
                return;
              }

            },
      error: function(XMLHttpRequest, textStatus, errorThrown) {
                if(XMLHttpRequest.status  == 403 ){
                  return;
                }

                console.log(XMLHttpRequest.status);
                console.log(XMLHttpRequest.readyState);
                console.log("aaa", textStatus);
            },

      complete: function(XMLHttpRequest, textStatus) {
                    this; // 调用本次AJAX请求时传递的options参数
            }
      });
  
  };


refreshbtn.onclick = (e) => {
    // file 转 blob对象
    let id = document.getElementById('pet-select');

    console.log("the select id :", id.value )
    if((id.value == "")){
      alert("you select ID ")
      return 
    }
    
    checkbox = document.getElementById('subscribeNews');
    getdetail()
    if (checkbox.checked ) {
        console.log("need excute cycle  10000")
        timebox = setInterval(getdetail, 40000);
    }else{
      console.log("now finish")
    }
};

updatebtn.onclick = (e) => {
  let id = document.getElementById('pet-select');
  console.log("the select id :", id.value )
  if((id.value == "")){
    alert("you select ID ")
    return 
  }

  keyword = document.getElementById("keyword");
  commentTime = document.getElementById("commenttime");
  limit_count = document.getElementById("limit_count");
  cookie = document.getElementById("cookie");
 
  $.ajax({
    type : "POST",
    url : 'update', 
    data : JSON.stringify({id:id.value, keyword:keyword.value, commentTime:commentTime.value, limit_count:limit_count.value, cookie:cookie.value}),
    contentType : "application/json;charset=utf-8",
    //dataType : "json",
    success : function(data){
            console.log("response",data);
            if(data['code'] == "success"){

              alert("更新成功，下个周期生效")
            }else{
              alert(data)
              return;
            }

          },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
              if(XMLHttpRequest.status  == 403 ){
                return;
              }
              console.log(XMLHttpRequest.status);
              console.log(XMLHttpRequest.readyState);
              console.log("aaa", textStatus);
          },

    complete: function(XMLHttpRequest, textStatus) {
                  this; // 调用本次AJAX请求时传递的options参数
          }
    });
};


})();
